package org.lanqiao.tjut.Test;

import java.util.List;
import java.util.Scanner;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.junit.Test;
import org.lanqiao.tjut.db.DBDriverFactory;
import org.lanqiao.tjut.entity.StinforEntity;

public class Student {
	Scanner input = new Scanner(System.in);
	StinforEntity s=new StinforEntity();
	int id;
	int psw;
	//接收者id
	int fi;
	//申请发起人id
	int fj;
	//接收信息者
	int mi;
	//发送信息者id
	int mj;
	//识别好友申请是否通过
	int f;
	//好友昵称
	String ni;
	//添加人昵称
	String nj;
	//留言
	String message;
	//被删除好友者id
	int a;
	//删除好友者id
	int b;
	@Test
	public void stInterface() {
		// 学生界面
		System.out.println("#####################################");
		System.out.println("#        1.查询个人信息                                           ");
		System.out.println("#        2.修改密码                                                 ");
		System.out.println("#        3.查询同学信息                                           ");
		System.out.println("#        4.添加好友                                                  ");
		System.out.println("#        5.显示我的所有好友信息                            ");
		System.out.println("#        6.保存好友信息                                           ");
		System.out.println("#        7.给好友留言                                               ");
		System.out.println("#        8.查看我的留言板                                       ");
		System.out.println("#        9.删除好友                                                   ");
		System.out.println("#        10.修改好友备注                                        ");
		System.out.println("#        0.任意键退出                                              ");
		System.out.println("######################################");
	}
    @Test
	public void doLog() {
		// 登录
		
		
		List lst;
        do{
        lst=null;
        System.out.println("请输入账号(若要返回上一级,请输入-1)：");
		id = input.nextInt();
		if(id==-1){
			Sys s=new Sys();
			s.chose();
		}
		System.out.println("请输入密码：");
		psw = input.nextInt();
		String strSQL = "SELECT * FROM STINFOR WHERE S_ID="+id+" AND S_PSW="+psw;
	    lst = (List) DBDriverFactory.getDBDriverInstance().
				query(strSQL, new MapListHandler());
        }while(lst.isEmpty());
        System.out.println("登陆成功");
        if(id==fi){
        	System.out.println("有帐号为"+fj+"的同学向你发送了添加好友请求");
        	System.out.println("输入1同意该好友请求,输入0拒绝:");
        	f=input.nextInt();
        	switch (f) {
			case 1:
				System.out.println("请输入备注姓名(昵称):");
			    nj=input.next();
				doAddfriends2();
				System.out.println("已同意该好友请求");
				break;
			case 0:
				System.out.println("已拒绝该好友请求");
				break;
			default:
				System.out.println("请重新输入");
				break;
			}
        	fi=0;
        	ni=null;
        }
        if(id==fj){
        	switch (f) {
			case 1:
				System.out.println("账号为"+fi+"的同学已经同意你的好友请求");
				break;
			case 0:
				System.out.println("很遗憾,你被拒绝了");
			default:
				break;
			}
        	fj=0;
        	nj=null;
        }
        if(id==a){
        	System.out.println("");
        }
		printList(lst);
	    if(psw==123){
	    	System.out.println("你的密码为默认值,如果不想被盗号就给老子改了");
	    	doPsw();
	    }
	}
    @Test
    public void doSelf() {
		//查询个人信息
    	System.out.println("---------1.查询个人信息----------");
    	String strB="select * from stinfor where s_id=?";
		   List lst2=(List)DBDriverFactory.getDBDriverInstance().query(strB,new MapListHandler(),new Object[]{id});
	       printList(lst2);

	}
    public void doPsw() {
		// TODO Auto-generated method stub
    	System.out.println("-----------2.修改密码--------------");
    	int a;
    	do{System.out.print("请输入原密码:");
    	a=input.nextInt();}while(a!=psw);
    	
    	int b;
	    int b0;
		do{System.out.print("请输入新密码:");
		b=input.nextInt();
		System.out.print("请再输入一遍:");
		b0=input.nextInt();}while(b!=b0);
		String str2="update stinfor set s_psw=? where s_id="+id;
	    int b1=DBDriverFactory.getDBDriverInstance().update(str2,new Object[]{b});
        System.out.println(b1>0?"修改成功":"修改失败");
        
	}
    @Test
    public void doClassmates() {
		// TODO Auto-generated method stub
    	System.out.println("---------3.查询同学信息----------");
	    System.out.println("a.查询所有学生信息");
	    System.out.println("b.根据指定编号查询单个学生信息");
	    System.out.println("c.根据姓名模糊查询学生信息");
		System.out.println("d.根据地址模糊查询学生信息");
		System.out.println("e.根据年龄段查询学生信息");
		System.out.print("请输入您要进行的操作(a.b.c.d.e):");
		char i=input.next().charAt(0);
		switch (i) {
		case 'a':
			System.out.println("a.查询所有学生信息");
			String strA="select s_id,s_name,s_sex,s_age,s_telephone,s_address from stinfor";
		List lst1=(List)DBDriverFactory.getDBDriverInstance().query(strA,new BeanListHandler(StinforEntity.class));
		printList(lst1);
			break;
		case 'b':
			System.out.println("b.根据指定编号查询单个学生信息");
			System.out.println("请输入学生账号:");
			int b1=input.nextInt();
			String strB="select s_id,s_name,s_sex,s_age,s_telephone,s_address from stinfor where s_id=?";
		   List lst2=(List)DBDriverFactory.getDBDriverInstance().query(strB,new MapListHandler(),new Object[]{b1});
	       printList(lst2);
		case 'c':
			 System.out.println("c.根据姓名模糊查询学生信息");
			System.out.println("请输入要查询的字段:");
			String c1=input.next();
			String strC="select s_id,s_name,s_sex,s_age,s_telephone,s_address from stinfor where s_name like ?";
		   List lst3=(List)DBDriverFactory.getDBDriverInstance().query(strC,new MapListHandler(),new Object[]{"%"+c1+"%"});
	       printList(lst3);
		case 'd':
			 System.out.println("d.根据地址模糊查询学生信息");
				System.out.println("请输入要查询的字段:");
				String d1=input.next();
				String strD="select s_id,s_name,s_sex,s_age,s_telephone,s_address from stinfor where s_address like ?";
			   List lst4=(List)DBDriverFactory.getDBDriverInstance().query(strD,new MapListHandler(),new Object[]{"%"+d1+"%"});
		       printList(lst4);
		case 'e':
			 System.out.println("e.根据年龄段查询学生信息");
				System.out.println("请输入最小年龄:");
				int e1=input.nextInt();
				System.out.println("请输入最大年龄:");
				int e2=input.nextInt();
				String strE="select s_id,s_name,s_sex,s_age,s_telephone,s_address from stinfor where s_age>="+e1+" and s_age<="+e2;
			   List lst5=(List)DBDriverFactory.getDBDriverInstance().query(strE,new MapListHandler());
		       printList(lst5);
		default:
			break;
		}
	}
    @Test
    public void doAddFriends() {
		// TODO Auto-generated method stub
    	System.out.println("---------4.添加好友----------");
    	String strA="SELECT *FROM STINFOR WHERE S_ID!=(SELECT S_ID FROM S"+id+")";
    	List lst=(List)DBDriverFactory.getDBDriverInstance().query(strA,new MapListHandler());
	       printList(lst);
	    System.out.println("请输入id并添加其为好友:");
	    fj=id;
	    fi=input.nextInt();
	    System.out.println("请输入备注姓名(昵称):");
	    ni=input.next();
	    System.out.println("已发送请求");
	}
    @Test
    public void doAddfriends2(){
    	//s_id为学生姓名,f+fj为根据id将好友id存入friend,已废弃
//    	String strSQL="update friend set f"+fj+"="+fj+" where s_id=?";
//		   int i=DBDriverFactory.getDBDriverInstance().update(strSQL,new Object[]{id});
//	       System.out.println(i>0?"添加成功":"添加失败");
//	       String strSQL1="update friend set f"+id+"="+id+" where s_id=?";
//		   int j=DBDriverFactory.getDBDriverInstance().update(strSQL1,new Object[]{fj});
//	       System.out.println(j>0?"添加成功":"添加失败");
    	String strSQL="INSERT INTO s"+fj+" SELECT * FROM STINFOR WHERE S_ID="+id;
		   int i=DBDriverFactory.getDBDriverInstance().update(strSQL);
	       System.out.println(i>0?"新增好友成功":"新增好友失败");
	       String strSQL1="update s"+fj+" set s_nickname='"+ni+"',s_psw=0 where s_id="+id;
		   int j=DBDriverFactory.getDBDriverInstance().update(strSQL1);
	       System.out.println(j>0?"好友信息处理成功":"好友信息处理失败");
	       String strSQL2="INSERT INTO s"+id+" SELECT * FROM STINFOR WHERE S_ID="+fj;
		   int k=DBDriverFactory.getDBDriverInstance().update(strSQL2);
	       System.out.println(k>0?"新增好友成功":"新增好友失败");
	       String strSQL3="update s"+id+" set s_nickname='"+nj+"',s_psw=0 where s_id="+fj;
		   int l=DBDriverFactory.getDBDriverInstance().update(strSQL3);
	       System.out.println(l>0?"好友信息处理成功":"好友信息处理失败");
	      
	       
    }
    @Test
    public void doShowFriends(){
    	System.out.println("----------5.显示我的所有好友信息-----------");
    	String strA="select * from s"+id;
    	List lst=(List)DBDriverFactory.getDBDriverInstance().query(strA,new MapListHandler());
	    printList(lst);
        
    }
    @Test
    public void doSaveFriends(){
    	System.out.println("----------6.保存好友信息-----------");
    	String strA="select * from s"+id;
    	List lst=(List)DBDriverFactory.getDBDriverInstance().query(strA,new MapListHandler());
	    printList(lst);
        System.out.println("以上为您所有好友的信息,使用复制或者截图功能可祝您保存他们的信息");
    }
    public void doMessage(){
    	System.out.println("--------7.给好友留言--------");
    	//全好友信息
    	String strA="select * from s"+id;
    	List lst=(List)DBDriverFactory.getDBDriverInstance().query(strA,new MapListHandler());
	       printList(lst);
	    System.out.println("请选择好友id并给其留言:");
	    mj=id;
	    mi=input.nextInt();
	    System.out.println("请输入留言:");
	    message=input.next();
	    System.out.println("留言已发送");
    }
    public void doMessage2(){
    	System.out.println("--------8.查看我的留言板--------");
    	 if(id==mi){
         	System.out.println("有帐号为"+mj+"的同学向你发送了留言:");
         	System.out.println(message);
         	message=null;
         	mi=0;
         	mj=0;
    	 }else{
    		 System.out.println("没有留言");
    	 }
    }
    @Test
    public void doDelete(){
    	System.out.println("---------9.删除好友----------");
    	//使用friend表删除好友,已废弃
//    	String strA="select * from friend where s_id=?";
//    	List lst=(List)DBDriverFactory.getDBDriverInstance().query(strA,new MapListHandler(),new Object[]{id});
//	    printList(lst);
//	    System.out.println("请输入要删除的好友的id:");
//	    int a=input.nextInt();
//	    String strSQL="update friend set f"+a+"="+null+" where s_id=?";
//		   int i=DBDriverFactory.getDBDriverInstance().update(strSQL,new Object[]{id});
//	       System.out.println(i>0?"删除成功":"添加失败");
//	       String strSQL1="update friend set f"+id+"="+null+" where s_id=?";
//		   int j=DBDriverFactory.getDBDriverInstance().update(strSQL1,new Object[]{a});
//	       System.out.println(j>0?"删除成功":"添加失败");
    	//全好友信息
    	String strA="select * from s"+id;
    	List lst=(List)DBDriverFactory.getDBDriverInstance().query(strA,new MapListHandler());
	    printList(lst);
	    System.out.println("请输入要删除的好友的id:");
	    a=input.nextInt();
	    b=id;
	    String strB="DELETE FROM S"+id+" WHERE S_ID="+a;
	    int i=DBDriverFactory.getDBDriverInstance().update(strB);
	    System.out.println(i>0?"删除成功":"删除失败");
	    String strC="DELETE FROM S"+a+" WHERE S_ID="+id;
		int j=DBDriverFactory.getDBDriverInstance().update(strC);
	    System.out.println(j>0?"删除成功":"删除失败");
    }
    @Test
    public void doNick() {
		//
    	System.out.println("-------10.修改好友备注(昵称)---------");
    	String strA="select * from s"+id;
    	List lst=(List)DBDriverFactory.getDBDriverInstance().query(strA,new MapListHandler());
	    printList(lst);
	    System.out.println("请输入要修改昵称的好友的id:");
	    int cid=input.nextInt();
    	System.out.print("请输入新昵称:");
		String newnick=input.next();
		String str="update s"+id+" set s_nickname=? where s_id="+cid;
	    int c1=DBDriverFactory.getDBDriverInstance().update(str,new Object[]{newnick});
        System.out.println(c1>0?"修改成功":"修改失败");
		System.out.println("修改后该好友信息:");
		String strB="select * from s"+id+" where s_id="+cid;
    	List lst1=(List)DBDriverFactory.getDBDriverInstance().query(strA,new MapListHandler());
	    printList(lst1);
	}
    private void printList(List lst) {
		
		System.out.println("list集合存储的二维表格内容：");
		for(int i=0;i<lst.size();i++){
			System.out.println(lst.get(i).toString());
		}
}
}
